﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace ServicioRFIDMovil
{
    public static class Extensor
    {
        #region --- Metodos de Encriptacion y Desencriptacion 3DES ---
        /// <summary>
        /// Funcion que permite de encriptar una cadena usando algoritmo 3DES
        /// </summary>
        /// <param name="data">Cadena a encriptar</param>
        /// <param name="llave">Llave que permitira encriptar</param>
        /// <returns>Retorna Cadena encriptada</returns>
        public static string encriptar3DES(this string data, string llave)
        {
            string claveEncriptada = string.Empty;
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

            byte[] bytKey = UTF8Encoding.UTF8.GetBytes(llave);

            DES.Mode = System.Security.Cryptography.CipherMode.ECB;
            DES.Key = bytKey;
            DES.Padding = PaddingMode.PKCS7;

            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            Byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(data);

            claveEncriptada = Encoding.Default.GetString(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

            return claveEncriptada.encriptarBase64();
        }

        /// <summary>
        /// Funcion que permite de desencriptar una cadena usando algoritmo 3DES
        /// </summary>
        /// <param name="data">Cadena a ser desencriptada</param>
        /// <param name="llave">Llave que permitira desencriptar</param>
        /// <returns>Retorna Cadena desencriptada</returns>
        public static string desEncriptarDES(this string data, string llave)
        {
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

            byte[] bytKey = UTF8Encoding.UTF8.GetBytes(llave);

            DES.Mode = System.Security.Cryptography.CipherMode.ECB;
            DES.Key = bytKey;
            DES.Padding = PaddingMode.PKCS7;

            ICryptoTransform DESDeEncrypt = DES.CreateDecryptor();

            Byte[] Buffer = Encoding.Default.GetBytes(data.desEncriptarBase64());

            return Encoding.Default.GetString(DESDeEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
        }
        #endregion

        #region --- Metodos de Encriptacion y Desencriptacion Base64 ---
        /// <summary>
        /// Funcion que permite encriptar una cadena en BASE 64
        /// </summary>
        /// <param name="data">Cadena a encriptar</param>
        /// <returns>Retorna Cadena encriptada</returns>
        public static string encriptarBase64(this string data)
        {
            byte[] bytes = Encoding.Default.GetBytes(data);
            return Convert.ToBase64String(bytes, 0, bytes.Length);
        }

        /// <summary>
        /// Funcion que permite de desencriptar una cadena en BASE 64
        /// </summary>
        /// <param name="data">Cadena a ser desencriptada</param>
        /// <returns>Retorna Cadena desencriptada</returns>
        public static string desEncriptarBase64(this string data)
        {
            byte[] bytes = Convert.FromBase64String(data);
            return Encoding.Default.GetString(bytes, 0, bytes.Length);
        }
        #endregion
    }
}
